package encaps.utils.search;

import encaps.action.*;
import encaps.entities.Person;
import encaps.entities.PersonLocal;
import encaps.entities.PersonLocalHome;
import encaps.entities.Role;
import encaps.resources.Grants;
import encaps.resources.Labels;
import encaps.resources.Response;
import encaps.utils.EntityUtil;
import encaps.utils.SystemException;
import encaps.utils.XMLHelper;
import java.util.Collection;
import javax.ejb.FinderException;
import javax.naming.NamingException;
import org.jdom.Element;

public class PersonSearcher extends Searcher {

    public PersonSearcher() {
        basicSearchHeaderPattern = Labels.getLabel(Labels.BASIC_PERSON_SEARCH_HEADER_PATTERN);
    }

    @Override
    public Element headersToXMLElement() {
        Element headers = XMLHelper.createElement(Response.COLUMN_HEADERS);

        Element nameHeader = XMLHelper.addChild(headers, Response.COLUMN_HEADER);
        XMLHelper.addChild(nameHeader, Response.COLUMN_HEADER_LABEL, Labels.getLabel(Labels.PERSON_NAME));
        XMLHelper.addChild(nameHeader, Response.COLUMN_NAME, Person.NAME);

        Element worldCasheader = XMLHelper.addChild(headers, Response.COLUMN_HEADER);
        XMLHelper.addChild(worldCasheader, Response.COLUMN_HEADER_LABEL, Labels.getLabel(Labels.PERSON_BIOGRAPHY));
        XMLHelper.addChild(worldCasheader, Response.COLUMN_NAME, Person.BIOGRAPHY);

        return headers;
    }

    @Override
    protected Element searchResultToTableRows(Collection persons, Role role) {
        Element rows = XMLHelper.createElement(Response.ROWS);
        for (Object obj : persons) {
            PersonLocal person = (PersonLocal) obj;
            Element row = XMLHelper.addChild(rows, Response.ROW);
            Element nameCell = XMLHelper.addChild(row, Response.CELL, person.getName());
            nameCell.setAttribute(Response.TYPE, Response.TEXT);

            Element bioCell = XMLHelper.addChild(row, Response.CELL);
            bioCell.addContent(person.getBio());
            bioCell.setAttribute(Response.TYPE, Response.TEXT);

            //Добавляем ссылки на действия над фильмами
            //Ссылка на просмотр
            Element viewLinkCell = XMLHelper.addChild(row, Response.CELL);
            viewLinkCell.setAttribute(Response.TYPE, Response.LINK);
            viewLinkCell.addContent(XMLHelper.createLink(Labels.getLabel(Labels.VIEW_LINK), ViewPersonAction.VIEW_PERSON, person.getId()));
            //Ссылка на редактирование
            if (role.hasGrant(Grants.EDIT_PERSON)) {
                Element editLinkCell = XMLHelper.addChild(row, Response.CELL);
                editLinkCell.setAttribute(Response.TYPE, Response.LINK);
                editLinkCell.addContent(XMLHelper.createLink(Labels.getLabel(Labels.EDIT_LINK), EditPersonAction.EDIT_PERSON, person.getId()));
            }

            if (role.hasGrant(Grants.DELETE_PERSON)) {
                Element deleteLinkCell = XMLHelper.addChild(row, Response.CELL);
                deleteLinkCell.setAttribute(Response.TYPE, Response.LINK);
                deleteLinkCell.addContent(XMLHelper.createLink(Labels.getLabel(Labels.DELETE_LINK), DeletePersonAction.DELETE_PERSON, person.getId()));
            }
        }
        return rows;
    }

    @Override
    protected Collection find(SearchContext context) throws SystemException {
        try {
            PersonLocalHome personHome = EntityUtil.getPersonHome();

            int recordsPerPage = context.getRecordsPerPage();
            int fromRecord = context.getFromRecord();
            Collection<PersonLocal> result = null;
            if (context.isBasicSearch()) {
                String searchParameter = context.getSearchParameter(BasicSearchAction.SEARCH_PARAMETER);
                result = personHome.findPaged(searchParameter,
                        context.getOrderColumn(), context.getOrderType(),
                        fromRecord, recordsPerPage);
            } else {
                String name = context.getSearchParameter(AdvancedPersonSearchAction.NAME);
                String bio = context.getSearchParameter(AdvancedPersonSearchAction.BIO);
                String movieTitle = context.getSearchParameter(AdvancedPersonSearchAction.MOVIE_TITLE);
                result = personHome.findByParametersPaged(name, bio, movieTitle,
                        context.getOrderColumn(), context.getOrderType(),
                        fromRecord, recordsPerPage);
            }
            return result;
        } catch (NamingException ex) {
            throw new SystemException(Labels.getLabel(Labels.CANNOT_FIND_LOCAL_HOME_OBJECT_MESSAGE), ex);
        } catch (FinderException ex) {
            throw new SystemException(ex);
        }

    }

    @Override
    protected int getFoundRecordsCount(SearchContext context) throws SystemException {
        try {
            PersonLocalHome personHome = EntityUtil.getPersonHome();
            int recordsCount = 0;
            if (context.isBasicSearch()) {
                recordsCount = personHome.getPersonsCount(context.getSearchParameter(BasicSearchAction.SEARCH_PARAMETER));
            } else {
                String name = context.getSearchParameter(AdvancedPersonSearchAction.NAME);
                String bio = context.getSearchParameter(AdvancedPersonSearchAction.BIO);
                String movieTitle = context.getSearchParameter(AdvancedPersonSearchAction.MOVIE_TITLE);
                recordsCount = personHome.getPersonsCount(name, bio, movieTitle);
            }
            return recordsCount;
        } catch (NamingException ex) {
            throw new SystemException(Labels.getLabel(Labels.CANNOT_FIND_LOCAL_HOME_OBJECT_MESSAGE), ex);
        } catch (FinderException ex) {
            //это не должно произойти
            throw new SystemException(ex);
        }
    }
}
